package com.starsoft.smdc.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.starsoft.smdc.bean.SmdcDiv;
import com.starsoft.smdc.bean.SmdcEr;
import com.starsoft.smdc.bean.SmdcMarketDaily;
import com.starsoft.smdc.bean.SmdcSecurity;

public class SecurityDataCombin {
	
	SmdcSecurity security;
	
	Map<Date, SecurityDataCombinBean> combinBeanMap = new HashMap<Date, SecurityDataCombinBean>();
	
	public SecurityDataCombin(SmdcSecurity security) {
		this.security = security;
	}


	public List<SecurityDataCombinBean> getList(){
		List<SecurityDataCombinBean> result = new ArrayList<>();
		for(SecurityDataCombinBean combinBean:combinBeanMap.values()){
			result.add(combinBean);
		}
		Collections.sort(result);
		return result;
	}
	
	
	public void combinMarketDaily(List<SmdcMarketDaily> marketList){	
		for(SmdcMarketDaily marketDaily:marketList){
			try {
			Date tradeDate = marketDaily.getId().getTradeDate();
			SecurityDataCombinBean combinBean =combinBeanMap.get(tradeDate);
			if(combinBean==null){
				combinBean = new SecurityDataCombinBean(tradeDate);
			}
			combinBean.setMarketDaily(marketDaily);
			combinBeanMap.put(tradeDate, combinBean);
			} catch (Exception e) {
				System.out.append(e.getMessage());
			}
		}
	}
	
	public void combinDiv(List<SmdcDiv> divList){	
		for(SmdcDiv div:divList){
			Date divDate = div.getId().getDivDate();
			SecurityDataCombinBean combinBean =combinBeanMap.get(divDate);
			if(combinBean==null){
				combinBean = new SecurityDataCombinBean(divDate);
			}
			combinBean.setDiv(div);
			combinBeanMap.put(divDate, combinBean);
		}
	}
	
	public void combinEr(List<SmdcEr> erList){	
		for(SmdcEr er:erList){
			Date reportDate = er.getId().getReportDate();
			SecurityDataCombinBean combinBean =combinBeanMap.get(reportDate);
			if(combinBean==null){
				combinBean = new SecurityDataCombinBean(reportDate);
			}
			combinBean.setEr(er);
			combinBeanMap.put(reportDate, combinBean);
		}
	}
	
	public class SecurityDataCombinBean implements Comparable<SecurityDataCombinBean>{
		Date dataDate;
		
		SmdcMarketDaily marketDaily;
		SmdcEr er;
		SmdcDiv div;
		
		public SecurityDataCombinBean(Date dataDate){
			this.dataDate = dataDate;
		}
		
		public Date getDataDate() {
			return dataDate;
		}
		public void setDataDate(Date dataDate) {
			this.dataDate = dataDate;
		}
		public SmdcMarketDaily getMarketDaily() {
			return marketDaily;
		}
		public void setMarketDaily(SmdcMarketDaily marketDaily) {
			this.marketDaily = marketDaily;
		}
		public SmdcEr getEr() {
			return er;
		}
		public void setEr(SmdcEr er) {
			this.er = er;
		}
		public SmdcDiv getDiv() {
			return div;
		}
		public void setDiv(SmdcDiv div) {
			this.div = div;
		}

		@Override
		public int compareTo(SecurityDataCombinBean o) {
			return this.dataDate.compareTo(o.getDataDate());
		}
	}
	
}
